package javabean;
import entity.person;
import entity.student;


import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;


public class audit {
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    public audit(){
    }
    public static void audit_local(String username,String password, HttpServletRequest request, HttpServletResponse response) throws IOException
    {

        int login_flag = 1;
        HttpSession session = request.getSession(true);
        request.getSession().removeAttribute("flag");
        try {
            Connection conn = connect.getConnect("jpa/DSTest");
            Statement stmt = conn.createStatement();
            ResultSet rs = conn.getMetaData().getTables(null,null,"person",null);
            if(rs!=null) {
                rs = stmt.executeQuery("select * from person");//选择import java.sql.ResultSet;

                while (rs.next()) {
                    if ((rs.getString("name")).equals(username) && rs.getString("password").equals(password)) {

                        login_flag = 0;

                        Map <String, String> login2 = new HashMap <String, String>();
                        login2.put("name", rs.getString("name"));
                        login2.put("email", rs.getString("email"));
                        login2.put("id", rs.getString("id"));
                        login2.put("img", rs.getString("img"));
                        login2.put("type", rs.getString("type"));
                        login2.put("faculty_title", rs.getString("faculty_title"));
                        login2.put("bname",rs.getString("bname"));
                        login2.put("bid",rs.getString("bid"));
                        login2.put("password",rs.getString("password"));
                        session.setAttribute("login2", login2);
                        break;

                    }

                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(login_flag == 0) {
            response.sendRedirect("/user.jsp");
        }
        else{
            String a="0";
            session.setAttribute("flag",a);
            response.sendRedirect("/index.jsp");
        }


    }
    public void audit_zhuce(String username, String password, String password2, String email, String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        int login_flag = 1;
        EntityManager em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        request.getSession().removeAttribute("flag");

        if (password.equals(password2)) {

            try {
                Connection conn = connect.getConnect("jpa/DSTest");
                Statement stmt = conn.createStatement();
                ResultSet rs = conn.getMetaData().getTables(null,null,"person",null);
                if(rs!=null) {

                    rs = stmt.executeQuery("select * from person");

                    while (rs.next()) {
                        if ((rs.getString("id")).equals(id)) {
                            login_flag = 0;
                            break;

                        }
                    }
                }
                    if (login_flag == 0)
                        response.sendRedirect("/error_zhuce.html");
                    else {

                        try {

                           person local=new person(id,username,email,"user.png","未绑定","未绑定",password,"未绑定","未绑定");

                            tx.begin();
                            em.persist(local);
                            tx.commit();

                        } catch (Exception ex) {
                            response.sendRedirect("/error.html");
                        } finally {
                            em.close();
                        }
                        response.sendRedirect("/index.jsp");
                    }

            } catch (SQLException e) {
                response.sendRedirect("/error.html");
            }
        }
        else{
            response.sendRedirect("/error_zhuce.html");
        }
    }






    public static void audit_m(student stu, HttpServletRequest request, HttpServletResponse response) throws IOException{
        int flag = 1;
        EntityManager em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        request.getSession().removeAttribute("flag");
        HttpSession session = request.getSession(true);
        Map <String, String> login1 = (Map <String, String>) request.getSession().getAttribute("login2");
        if (login1!= null) {
            try {
                Connection conn = connect.getConnect("jpa/DSTest");
                String id = login1.get("id");
                Statement stmt = conn.createStatement();
                String deleteSql = "delete from person where id=" + id;
                stmt.executeUpdate(deleteSql);

                try{
                person per = new person(login1.get("id"), login1.get("name"), login1.get("email"), login1.get("img"), stu.getFaculty_title(), stu.getGroup(), login1.get("password"), stu.getName(), stu.getUsername());
                tx.begin();
                em.persist(per);
                tx.commit();

                Map <String, String> login2 = new HashMap <String, String>();
                login2.put("id", per.getId());
                login2.put("name", per.getName());
                login2.put("email", per.getEmail());
                login2.put("img", per.getImg());
                login2.put("type", per.getType());
                login2.put("faculty_title", per.getFaculty_title());
                login2.put("bname", per.getBname());
                login2.put("bid", per.getBid());
                login2.put("password",per.getPassword());
                session.setAttribute("login2", login2);

            } catch (Exception ex) {
                response.sendRedirect("/error.html");
            } finally {
                em.close();
            }
            response.sendRedirect("/user.jsp");
            } catch (SQLException e) {
                    e.printStackTrace();
                }

        } else {
            try {
                Connection conn = connect.getConnect("jpa/DSTest");
                Statement stmt = conn.createStatement();
                ResultSet rs = conn.getMetaData().getTables(null, null, "person", null);
                if (rs != null) {
                    rs = stmt.executeQuery("select * from person");//选择import java.sql.ResultSet;
                    while (rs.next()) {
                        if ((rs.getString("id")).equals(stu.getUsername())) {
                            flag = 0;
                            break;
                        }

                    }
                }
                if (flag == 0) {
                    audit.audit_local(stu.getName(), "123456", request, response);
                } else {

                    try {

                        person per = new person(stu.getUsername(), stu.getName(), "123@qq.com", "user.png", stu.getFaculty_title(), stu.getGroup(), "123456", stu.getName(), stu.getUsername());
                        tx.begin();
                        em.persist(per);
                        tx.commit();

                        Map <String, String> login2 = new HashMap <String, String>();
                        login2.put("id", per.getId());
                        login2.put("name", per.getName());
                        login2.put("email", per.getEmail());
                        login2.put("img", per.getImg());
                        login2.put("type", per.getType());
                        login2.put("faculty_title", per.getFaculty_title());
                        login2.put("bname", per.getBname());
                        login2.put("bid", per.getBid());
                        login2.put("password",per.getPassword());
                        session.setAttribute("login2", login2);


                    } catch (Exception ex) {
                        response.sendRedirect("/error.html");
                    } finally {
                        em.close();
                    }
                    response.sendRedirect("/user.jsp");
                }


            } catch (SQLException e) {
                e.printStackTrace();
            }


        }

    }





}
